package com.zp.drug.controller;

import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.drug.entity.bo.DrugExportDetailBo;
import com.zp.drug.entity.vo.DrugExportDetailVo;
import com.zp.drug.service.IDrugExportDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 药品出库明细控制器
 * 前端访问路由地址为:/drug/drugExportDetail
 *
 * @author zhang peng
 * @ date 2024-05-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/drugExportDetail")
public class DrugExportDetailController extends BaseController {

    private final IDrugExportDetailService iDrugExportDetailService;

    /**
     * 查询药品出库明细列表 (分页)
     */
    @GetMapping("/selectPage")
    public TableDataInfo<DrugExportDetailVo> selectPage(DrugExportDetailBo bo, PageQuery pageQuery) {
        return iDrugExportDetailService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询药品出库明细列表 (不分页)
     */
    @GetMapping("/list")
    public R<List<DrugExportDetailVo>> list(DrugExportDetailBo bo) {
        List<DrugExportDetailVo> list = iDrugExportDetailService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出药品出库明细列表
     */
    @Log(title = "药品出库明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DrugExportDetailBo bo, HttpServletResponse response) {
        List<DrugExportDetailVo> list = iDrugExportDetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "药品出库明细", DrugExportDetailVo.class, response);
    }

    /**
     * 获取药品出库明细详细信息
     *
     * @param detailId 主键
     */
    @GetMapping("/{detailId}")
    public R<DrugExportDetailVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String detailId) {
        return R.ok(iDrugExportDetailService.queryById(detailId));
    }

    /**
     * 新增药品出库明细
     */
    @Log(title = "药品出库明细", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DrugExportDetailBo bo) {
        return toAjax(iDrugExportDetailService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改药品出库明细
     */
    @Log(title = "药品出库明细", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DrugExportDetailBo bo) {
        return toAjax(iDrugExportDetailService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除药品出库明细
     *
     * @param detailIds 主键串
     */
    @Log(title = "药品出库明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{detailIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] detailIds) {
        return toAjax(iDrugExportDetailService.deleteWithValidByIds(Arrays.asList(detailIds), true) ? 1 : 0);
    }
}
